package com.hs.srm.base.common.web;

import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;

import sunnykid.lang.SunnykidDate;

/**
 * springMVC属性值绑定（日期类型转成Timestamp）
 *
 * @author Sunnykid
 * @date 2018年8月14日 下午3:19:36
 */
public class CustomTimestampEditor extends PropertyEditorSupport {

	@Override
	public String getAsText() {
		if(this.getValue() == null) {
			return null;
		}
		Timestamp value = (Timestamp)this.getValue();
		SunnykidDate sDate = new SunnykidDate(value.getTime());
		return ((value != null) ? sDate.getStandardDate() : null);
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if(text == null || text.trim().length() == 0x0) {
			this.setValue(null);
			return;
		}
		try {
			SunnykidDate sDate = SunnykidDate.parseSunnykidDate(text);
			this.setValue(sDate.toTimestamp());
		} catch(Exception ex) {
			throw new IllegalArgumentException("Could not parse Timestamp: " + ex.getMessage(), ex);
		}
	}

}
